<script setup>
import { useCartStore, useOrderStore, useUserStore } from '@/stores'
import showPrompt from '@/utils/showPrompt'
import ReturnButton from '@/components/ReturnButton.vue'
import SingleButton from '@/components/SingleButton.vue'
import { formatDate } from '@/utils/formatDate'
import { computed, ref, watch } from 'vue'
const cartStore = useCartStore()
const orderStore = useOrderStore()
const userStore = useUserStore()

const filterCartList = computed(() =>
  cartStore.items.filter(item => item.userId === userStore.userId)
)
const cartList = ref(filterCartList.value)

watch(
  () => cartStore.items,
  () => (cartList.value = filterCartList.value),
  { deep: true }
)

const pay = () => {
  if (!userStore.userInfo.address) {
    return showPrompt('未填写收货地址', 'error')
  }

  const goodsList = cartList.value.map(item => ({
    name: item.name,
    price: item.price * item.quantity,
    quantity: item.quantity
  }))

  // 添加到订单
  orderStore.setOrderList({
    name: userStore.userInfo.name,
    totalPrice: cartStore.totalPrice,
    orderTime: formatDate(new Date()),
    status: '已支付',
    goodsList1: goodsList,
    address: userStore.userInfo.address
  })
  showPrompt('结算成功', 'success')
  cartStore.clearCart()
}
</script>

<template>
  <div class="cart-container">
    <ReturnButton />
    <h1>购物车</h1>
      <div v-if="cartList.length === 0" class="empty-order">
      <p>购物车空空如也</p>
      <router-link to="/">去购物</router-link>
    </div>

   
    <div v-else>
      <div class="cart-header">
        <div class="header-item">商品</div>
        <div class="header-item">单价</div>
        <div class="header-item">数量</div>
        <div class="header-item">小计</div>
        <div class="header-item">操作</div>
      </div>

      <!-- 遍历购物车中的商品 -->
      <div v-for="item in cartList" :key="item.id" class="cart-item">
        <div class="item-info">{{ item.name }}</div>
        <div class="item-price">¥{{ item.price }}</div>

        <div class="item-quantity">
          <button @click="cartStore.decreaseQuantity(item)">-</button>
          <span>{{ item.quantity }}</span>
          <button @click="cartStore.increaseQuantity(item)">+</button>
        </div>

        <div class="item-subtotal">¥{{ (item.price * item.quantity).toFixed(2) }}</div>

        <div class="item-action">
          <!-- 使用商品ID删除 -->
          <button @click="cartStore.removeItem(item.id)" class="remove-btn">删除</button>
        </div>
      </div>

      <div class="cart-footer">
        <div class="total-price">
          总计: <strong>¥{{ cartStore.totalPrice }}</strong>
        </div>
        <div class="action-buttons">
          <SingleButton text="结算" @click="pay" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.empty-order {
  text-align: center;
  padding: 60px 0;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
  margin: 30px 0;
  border: 1px dashed #eee;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  }

  svg {
    margin-bottom: 20px;
    color: #ccc;
  }

  p {
    margin-bottom: 25px;
    font-size: 18px;
    color: #888;
  }

  a {
    display: inline-block;
    background: var(--theme-color);
    color: black;
    padding: 10px 28px;
    border-radius: 30px;
    text-decoration: none;
    font-weight: 600;
    font-size: 16px;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(255, 250, 107, 0.25);

    &:hover {
      color: #fff;
      background-color: #ff6b6b;
      transform: translateY(-2px);
      box-shadow: 0 6px 15px rgba(255, 107, 107, 0.35);
    }
  }
}
.cart-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

h1 {
  font-size: 28px;
  font-weight: 700;
  margin: 20px 0 30px;
  color: #333;
  text-align: center;
  position: relative;

  &::after {
    content: '';
    display: block;
    width: 60px;
    height: 3px;
    background: var(--theme-color);
    margin: 10px auto 0;
    border-radius: 2px;
  }
}

.empty-cart {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 18px;
}

.cart-header,
.cart-item {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr 1fr;
  gap: 15px;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
  align-items: center;
  padding-left: 20px;
}

.cart-header {
  font-weight: bold;
  background-color: #f8f8f8;
  border-top: 1px solid #eee;
  padding-left: 20px;
}
.item-quantity button {
  width: 28px;
  height: 28px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  border-radius: 4px;
}

.item-quantity span {
  margin: 0 10px;
}

.remove-btn {
  background: #ff4d4f;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background-color: #d74245;
  }
}

.cart-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 20px 0;
  margin-top: 20px;
}

.total-price {
  margin-right: 30px;
  font-size: 18px;
}

@media (max-width: 600px) {
  .cart-header,
  .cart-item {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      'name name'
      'price quantity'
      'subtotal action';
  }

  .item-info {
    grid-area: name;
  }

  .item-price {
    grid-area: price;
  }

  .item-quantity {
    grid-area: quantity;
  }

  .item-subtotal {
    grid-area: subtotal;
  }

  .item-action {
    grid-area: action;
  }
}
.action-buttons {
  display: flex;
  gap: 15px;
}

.order-link {
  display: inline-block;
  padding: 10px 20px;
  background: #f0f0f0;
  color: #333;
  border-radius: 4px;
  text-decoration: none;

  &:hover {
    background: #e0e0e0;
  }
}
</style>
